define([
  'jquery',
  'underscore',
  'backbone',
  'backbonepoller',
  'collections/feeds/FeedsCollection',
], function($, _, Backbone,BackbonePoller,FeedsCollection){

  var SidebarView = Backbone.View.extend({
    el: $(".control"),
    events: {'click' : 'navigate'} ,
    initialize: function(){
    },
    render: function(presetData){
        if($('#usertype').val()=='D'){
        	$('.control').html("<span class=\"sidebar\"><img src=\"/assets/images/left_panel_login_doctor.png\"></span>");
        }else if($('#usertype').val()=='P'){
        	$('.control').html("<span class=\"sidebar\"><img src=\"/assets/images/left_panel_login_patient.png\"></span>");
        }else if($('#usertype').val()=='S'){
        	$('.control').html("<span class=\"sidebar\"><img src=\"/assets/images/left_panel_login_super.png\"></span>");
        }else if($('#usertype').val()=='L'){
        	$('.control').html("<span class=\"sidebar\"><img id=\"sidebarimg\" src=\"/assets/images/left_panel_login_lab.png\"><canvas id=\"canvas1\" width=\"30\" height=\"30\" /></span>");
        	
    	    var options = {
      	    	    delay: 60000  	    	   
      	    	};
    	    var feedsCollection=new FeedsCollection();
        	var poller = Backbone.Poller.get(feedsCollection,options).start();
        	if(!this.latestTest){
            	var d = new Date();
            	this.latestTest=d.getTime();	  
            	this.lastTest = null;
        	}
        	var self=this;
        	poller.on('success', function(model){        		
        		var count=0;
        		var m = model.models;
        		for(var i in m){
        			if(self.latestTest<m[i].get("updatedOn")){
        				console.log(self.latestTest + " "+m[i].get("updatedOn"));
        				count++;
        			}
        		}
        		if(count>0){
               		var latestTest = model.models[0].get("updatedOn");
            		if(self.latestTest<latestTest){
            			self.lastTest= self.latestTest;
            			self.latestTest=latestTest;
            		}
        			var can = document.getElementById('canvas1');
    	  			var context = can.getContext('2d');	
        			context.fillStyle = "red";
	    			context.beginPath();
	    			var radius = 8; 
	    			context.arc(15, 15, radius, 0, Math.PI * 2);
	    			context.closePath();
	    			context.fill();
	    			context.fillStyle = "black"; // font color to write the text with
	    			var font = "bold " + radius +"px serif";
	    			context.font = font;
        			// Move it down by half the text height and left by half the text width
        			var width = context.measureText(count).width;
        			var height = context.measureText("w").width; // this is a GUESS of height
        			context.fillText(count, 15 - (width/2) ,15 + (height/2));

        			// To show where the exact center is:
        			context.fillRect(15,15,1,1);
        			$("#canvas1").css({top:'10',left:$('#sidebarimg').width()-20,'z-index':'999','position':'absolute'});
        		}
 

        	});
        }
  
    },
    navigate: function(e){
  		var x = e.pageX - $(e.currentTarget).offset().left;
  		var y = e.pageY - $(e.currentTarget).offset().top;
        if($('#usertype').val()=='D'){
        	this.navigateDoctor(x,y);
        }
        if($('#usertype').val()=='P'){
        	this.navigatePatient(x,y);
        }	
        if($('#usertype').val()=='S'){
        	this.navigateSuper(x,y);
        }	
        if($('#usertype').val()=='L'){
        	this.navigateLab(x,y);
        }	

    },
    navigateDoctor: function(x,y){
    	var left =$(".mainpanel").offset().left;
  		if(x > 0 && x< 100){
  			if(y >0 && y < 100){
	  			$(".leftarrowdiv").css({top:'190px',left:left,'z-index':'999'});
				$(".leftarrowdiv").show();
	  			this.goTo( "activity");

  			}
  			if(y >101 && y < 204){
  				$(".leftarrowdiv").css({top:'280px',left:left,'z-index':'999'});
  				$(".leftarrowdiv").show();
	  			this.goTo("referPatient");

  			}
  			if(y >205 && y < 303){
  				$(".leftarrowdiv").css({top:'380px',left:left,'z-index':'999'});
  				$(".leftarrowdiv").show();
	  			this.goTo("schedule");

  			}
  			if(y >304 && y < 400){
  				//this.showloginframesch();
  				$(".leftarrowdiv").css({top:'480px',left:left,'z-index':'999'});
  				$(".leftarrowdiv").show();
  				this.goTo("addProfiles");
  			}
  			if(y >401 && y < 500){
	  			$(".leftarrowdiv").css({top:'580px',left:left,'z-index':'999'});
	  			$(".leftarrowdiv").show();
	  			this.goTo( "manageResponse");
  			}
  			if(y >501 && y < 600){
	  			$(".leftarrowdiv").css({top:'680px',left:left,'z-index':'999'});
	  			$(".leftarrowdiv").show();
	  			this.goTo("history");

  			}
  			if(y >601 && y < 700){
	  			$(".leftarrowdiv").css({top:'780px',left:left,'z-index':'999'});
	  			$(".leftarrowdiv").show();
	  			this.goTo( "usersettings");

  			}
  			if(y >701 && y < 800){
	  			$(".leftarrowdiv").css({top:'880px',left:left,'z-index':'999'});
	  			$(".leftarrowdiv").show();
	  			this.goTo( "doccharts");

  			}
  		}

    	
    },
    navigatePatient: function(x,y){
    	var left =$(".mainpanel").offset().left;
  		if(x > 0 && x< 100){
  			if(y >0 && y < 130){
	  			$(".leftarrowdiv").css({top:'200px',left:left,'z-index':'999'});
				$(".leftarrowdiv").show();
	  			this.goTo( "activity");

  			}
  			if(y >131 && y < 265){
  				$(".leftarrowdiv").css({top:'340px',left:left,'z-index':'999'});
  				$(".leftarrowdiv").show();
	  			this.goTo("search");

  			}
  			if(y >266 && y < 405){
  				//this.showloginframesch();
  				$(".leftarrowdiv").css({top:'480px',left:left,'z-index':'999'});
  				$(".leftarrowdiv").show();
  				this.goTo("patientHistory");
  				
  			}
  			if(y >406 && y < 530){
	  			$(".leftarrowdiv").css({top:'600px',left:left,'z-index':'999'});
	  			$(".leftarrowdiv").show();
	  			this.goTo( "usersettings");

  			}

  		}

    },
    navigateSuper: function(x,y){
    	var left =$(".mainpanel").offset().left;
  		if(x > 0 && x< 100){
  			if(y >0 && y < 130){
	  			$(".leftarrowdiv").css({top:'200px',left:left,'z-index':'999'});
				$(".leftarrowdiv").show();
	  			this.goTo( "admingraphs");

  			}
  			if(y >131 && y < 265){
  				$(".leftarrowdiv").css({top:'295px',left:left,'z-index':'999'});
  				$(".leftarrowdiv").show();
	  			this.goTo("approveusers");

  			}

  		}

    },
    navigateLab: function(x,y){
    	var left =$(".mainpanel").offset().left;
  		if(x > 0 && x< 100){
  			if(y >0 && y < 130){
	  			$(".leftarrowdiv").css({top:'200px',left:left,'z-index':'999'});
				$(".leftarrowdiv").show();
	  			this.goTo( "activity");
	  			var can = document.getElementById('canvas1');
	  			var context = can.getContext('2d');	  			
	  			context.clearRect(0, 0, can.width, can.height);
  			}
			//self.latestTest=feedsCollection.models[0].get("updatedOn");
  		}

    }

 });

  return SidebarView;
});
